<template>
  <div>
    <h2>课程管理</h2>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item to="/AdminMain/AdminHome">主页</el-breadcrumb-item>
      <el-breadcrumb-item>课程管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card style="margin-top: 20px;">
      <template #header>
        <div class="card-header">
          <i class="el-icon-s-order" style="margin-right: 5px;"></i>课程信息表
        </div>
      </template>

      <el-table
          :data="tableData"
          style="width: 100%;"
          v-loading="loading"
          empty-text="暂无课程数据"
      >
        <el-table-column prop="classId" label="编号" />
        <el-table-column prop="className" label="名称" />
        <el-table-column prop="classBegin" label="时间" />
        <el-table-column prop="classTime" label="时长" />
        <el-table-column prop="coach" label="教练" />
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <div class="action-buttons">
              <el-button type="info" size="small" @click="onOrder(scope.row)">报名信息</el-button>
              <el-button type="danger" size="small" @click="onDelete(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-pagination
          style="margin-top: 20px; text-align: center"
          background
          :current-page="currentPage"
          :page-size="pageSize"
          :total="total"
          layout="total, prev, pager, next, jumper"
          @current-change="handlePageChange"
      >
        <template #total>
          共 {{ total }} 条
        </template>
        <template #jumper>
          跳转到
          <el-input
              class="el-pagination__editor is-in-pagination"
              v-model.number="jumperPage"
              type="number"
              size="small"
              @keyup.enter.native="jumpToPage"
              :min="1"
              :max="Math.ceil(total / pageSize)"
          />
          页
        </template>
      </el-pagination>

      <div style="text-align:center;margin-top:20px;">
        <el-button type="success" @click="onAdd">添加课程信息</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'

const router = useRouter()

const tableData = ref([])
const loading = ref(false)

// 分页状态
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const jumperPage = ref(null)

// 加载课程数据
const loadData = () => {
  loading.value = true
  axios.get('/server/class/search', {
    params: {
      page: currentPage.value,
      size: pageSize.value
    }
  })
      .then(response => {
        if (response.data.code === 200) {
          tableData.value = response.data.data.records
          total.value = response.data.data.total
        } else {
          ElMessage.error('获取数据失败: ' + (response.data.message || '未知错误'))
        }
      })
      .catch(error => {
        console.error('获取课程数据失败:', error)
        ElMessage.error('加载课程数据失败')
      })
      .finally(() => {
        loading.value = false
      })
}

// 页面挂载后加载数据
onMounted(() => {
  loadData()
})

// 页码切换
const handlePageChange = (newPage) => {
  currentPage.value = newPage
  loadData()
}

// 跳转到页
const jumpToPage = () => {
  const maxPage = Math.ceil(total.value / pageSize.value)
  if (jumperPage.value >= 1 && jumperPage.value <= maxPage) {
    currentPage.value = jumperPage.value
    loadData()
  } else {
    ElMessage.warning('请输入有效页码')
  }
}

// 跳转到报名信息
const onOrder = (row) => {
  router.push({
    path: '/AdminMain/ClassOrder',
    query: { classId: row.classId }
  })
}

// 删除课程
const onDelete = (row) => {
  ElMessageBox.confirm(
      `确定要删除编号为 ${row.classId} 的课程吗？`,
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(() => {
    axios.delete(`/server/class/delete?class`,{params:{classId: row.classId}})
        .then(res => {
          if (res.data.code === 200) {
            ElMessage.success('删除成功')
            loadData()
          } else {
            ElMessage.error('删除失败：' + (res.data.message || '未知错误'))
          }
        })
        .catch(() => {
          ElMessage.error('请求失败')
        })
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 添加课程
const onAdd = () => {
  router.push('/AdminMain/AddClass')
}
</script>

<style scoped>
.card-header {
  font-weight: bold;
}

.action-buttons {
  display: flex;
  gap: 8px;
  justify-content: flex-start;
  align-items: center;
}
</style>
